package kgc.crm.controller;

import kgc.crm.annotation.UserPower;
import kgc.crm.annotation.VerificationToken;
import kgc.crm.enums.PowerEnum;
import kgc.crm.model.AdminField;
import kgc.crm.model.AdminFieldv;
import kgc.crm.model.Leads;
import kgc.crm.service.ILeadsService;
import kgc.crm.service.impl.LeadsServiceImpl;
import kgc.crm.service.impl.NewLeadsServiceImpl;
import kgc.crm.util.RequestUtil;
import kgc.crm.vo.LeadsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RequestMapping("lead")
@CrossOrigin
@RestController
public class LeadsController {
    @Resource(type = LeadsServiceImpl.class,name = "leadsServiceImpl")
    private ILeadsService leadsService;
    @Resource(type = NewLeadsServiceImpl.class,name = "newLeadsServiceImpl")
    private ILeadsService newLeadsService;

    //查询方法 查询列表 需要分页 需要自定义 where条件 需要search
    @UserPower(power = PowerEnum.INDEX)
    @VerificationToken
    @PostMapping("list")
    public ResponseEntity list(@RequestBody LeadsVo vo,HttpServletRequest request) {
        String apiToken = RequestUtil.getApiToken(request);
        return  leadsService.list(vo,apiToken);
    }
    /*-----------------------------*/
    @UserPower(power = PowerEnum.READ)
    @VerificationToken
    @PostMapping("read")
    public ResponseEntity read(@RequestBody String batchId,HttpServletRequest request) {
        String apiToken = RequestUtil.getApiToken(request);
        return newLeadsService.read(batchId,apiToken);
    }
    //修改方法
    @VerificationToken
    @UserPower(power = PowerEnum.UPDATE)
    @PostMapping("update")
    public ResponseEntity update(HttpServletRequest request,
                                   @RequestBody Leads leads) {
        String apiToken = RequestUtil.getApiToken(request);
        return leadsService.update(apiToken,leads);
    }
    //添加
    @VerificationToken
    @UserPower(power = PowerEnum.SAVE)
    @PostMapping("add")
    public ResponseEntity addLeads(HttpServletRequest request,
                                   @RequestBody Leads leads,String types) {
        String apiToken = RequestUtil.getApiToken(request);
        return leadsService.addLeads(leads,apiToken,types);
    }
    //添加
    @VerificationToken
    @UserPower(power = PowerEnum.INDEX)
    @PostMapping("field/list")
    public ResponseEntity selectFieldList(@RequestBody AdminField field) {
        return leadsService.selectFieldList(field.getLabel());
    }


    //查询线索跟进记录
    @UserPower(power = PowerEnum.READ)
    @VerificationToken
    @PostMapping("record/search")
    public ResponseEntity recordSearch(@RequestBody Leads leads,HttpServletRequest request) {
        return leadsService.recordSearch(leads.getLeadsId(),RequestUtil.getApiToken(request));
    }
}
